package com.android.semita.controller;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.semita.model.Invoice;
import com.android.semita.model.Messenger;
import com.android.semita.model.MessengerJob;

public class MessengerController {
	
	public ArrayList<Invoice> getInvoicesFromJSON(String JSONString)
	{
		ArrayList<Invoice> invoices = new ArrayList<Invoice>();

		try {
			JSONObject invoiceJSONObject;
			invoiceJSONObject = new JSONObject(JSONString);
			
			JSONArray invoiceArray = invoiceJSONObject.getJSONArray("InvoiceHead");			
			
			for(int i=0; i<=invoiceArray.length() -1; i++)
			{
				JSONObject invoiceItemObject = invoiceArray.getJSONObject(i);
				
				Invoice invoice = new Invoice();
				
				invoice.setInvoiceNo(invoiceItemObject.getString("InvoiceNo"));
				invoice.setCustomerName(invoiceItemObject.getString("CustName"));
				invoice.setInvoiceAmount(invoiceItemObject.getLong("Amount"));
				
				invoices.add(invoice);
			}

			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		return invoices;
	}
	
	public Messenger getMessengerFromJSON(String JSONString)
	{
		

		try {
			JSONObject invoiceJSONObject;
			invoiceJSONObject = new JSONObject(JSONString);
			
			JSONArray messengerArray = invoiceJSONObject.getJSONArray("InvoiceHead");			
			
			JSONObject messengerObject = messengerArray.getJSONObject(0);
				
			
			Messenger messenger = new Messenger();
			
			messenger.setId(Integer.parseInt(messengerObject.getString("Id")));
			messenger.setUserName(messengerObject.getString("Username"));
			messenger.setDescription(messengerObject.getString("UserDesc"));
			messenger.setFirstName(messengerObject.getString("FirstName"));
			messenger.setLastName(messengerObject.getString("LastName"));
			
			return messenger;	
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
